﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rabbit.Model;
using System.Threading;
using System.Diagnostics; 

namespace Rabbit.BusinessLogicLayer
{
    public class PublicStatusBL : AutoUpdateMessageClassBase<Status>
    {
        public PublicStatusBL(double UpdateInterval)
            : base(UpdateInterval)
        {

        }

        #region GetPublicStatus

        private static List<Status> GetPublicStatuses()
        {
            string statusString = RequestHelper.SendOAuthRequest("statuses/public_timeline.json", "");
            List<Status> publicStatuses = WrappedJsonHelper.Deserialize<List<Status>>(statusString);

#if DEBUG
            string userlistPath = "userlist.txt";
            List<string> userlist = new List<string>();
            if (!System.IO.File.Exists(userlistPath))
            {
                System.IO.File.Create(userlistPath);
            }
            else
            {
                userlist.AddRange(System.IO.File.ReadAllLines(userlistPath).AsEnumerable());
            }

            int sleepSeconds = 10*1000;
            List<string> replyContent = new List<string>();

            replyContent.Add("写真.帅呆");
            replyContent.Add("工作..hi{0},最近工作怎么样");
            replyContent.Add("股票..我对股票一窍不通啊。。//@{0}");
            replyContent.Add("证券..我对股票啊证券啊之类的一窍不通啊。。//@{0}");
            replyContent.Add("累啊..我也累啊，人生下来就是受罪的//@{0}");
            replyContent.Add("受罪..我也累啊，人生下来就是受罪的//@{0}");
            replyContent.Add("拍电影..您是演员啊{0}");
            replyContent.Add("客户端..你喜欢使用客户端啊？我在使用自己开发的微兔客户端，建议您也使用使用呗//@{0}");
            replyContent.Add("给力..[给力],嘿嘿");
            replyContent.Add("哭..男人哭吧哭吧不是罪，女人哭吧哭吧会变美,哈哈哈");
            replyContent.Add("简单..提到简单我想到了没那么简单这首歌，我以前一直以为原唱是个男的。。");
            replyContent.Add("工资..悲剧的工资,[哭]");
            replyContent.Add("纳税..悲剧的纳税人啊，我享受了啥了没有？");
            replyContent.Add("好成绩..还在上学啊{0}");
            replyContent.Add("减肥..我一直想增肥来着，可就是光吃不肥[哭]");
            replyContent.Add("辣椒..最不能吃的就是它了,友情问候{0}");
            replyContent.Add("饮食..每天为饮食而发愁{0}");
            replyContent.Add("键盘..您是键盘党么？{0}");
            replyContent.Add("乞丐..这年头，乞丐也赚钱比我们所谓的“白领“多，是吧{0}");
            replyContent.Add("早饭..天天早饭不知道吃啥呀{0}");
            replyContent.Add("政府..正腐，哈哈");
            replyContent.Add("中奖..我也天天参与抽奖，可是一次没中过[哭]");
            replyContent.Add("小家碧玉..小家碧玉，我喜欢[色]");
            replyContent.Add("网上购物..网上购物不得不说还是很方便的[色]");
            replyContent.Add("抽烟..一直不会抽烟，不想学习抽烟");
            replyContent.Add("踏青..现在正是踏青的好日子啊，可惜没时间去");
            replyContent.Add("阳光..阳光，我喜欢");
            replyContent.Add("伤不起..伤不起啊伤不起伤不起@{0}");
            replyContent.Add("外星人..你说有没有外星人[调皮]，不要告诉我让我去问美国宇航局呀@{0}");
            replyContent.Add("开心..有没有啥开心的事说出来让大家开心一下，有没有不开心的事，说出来也让大家开心一下[调皮]{0}");
            replyContent.Add("城管..城管就是流氓恶霸的代名词了{0}");
            replyContent.Add("激情..[色]");
            replyContent.Add("一头撞死..可别介呀{0}");
            replyContent.Add("腰疼..这是怎么回事涅[调皮]{0}");
            replyContent.Add("屁民..屁民..");

            Action autoComment = new Action(() => {
                foreach (var item in publicStatuses)
                {
                    try
                    {
                        if (sleepSeconds > 20000)
                        {
                            sleepSeconds-=10000;
                        }

                        if (item.User.StatusesCount>=50 && !userlist.Exists(s=>s==item.User.ScreenName))
                        {
                            int index = replyContent.FindIndex(s => item.Text.Contains(s.Split('.')[0]));
                            if (index>=0)
                            {
                                StatusesBL.ReplyToStatus(string.Format(replyContent[index], item.User.ScreenName), item.Id);
                                userlist.Add(item.User.ScreenName);
                                System.IO.File.WriteAllLines(userlistPath,userlist);

                                Debug.Print(
                                    string.Format("comment on {0} " +
                                      "'s status, text: {1} comment content:{2}" +
                                      " thread.sleep:{3}",
                                      item.User.ScreenName,
                                      item.Text,
                                      replyContent[index],
                                      sleepSeconds
                                      ));
                                Thread.Sleep(sleepSeconds);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        sleepSeconds+=10000;
                    }
                }
            });
            autoComment.BeginInvoke(null, null);
#endif
            StatusesBL.GetForwardedStatusPicture(publicStatuses);
            return publicStatuses;
        }
        #endregion

        protected override void FetchNewMessage()
        {
            List<Status> newStatusList = GetPublicStatuses();
            newStatusList = newStatusList.TakeWhile(s => s.Id > LastMaxID).ToList();
            if (newStatusList.Count>0)
            {
                LastMaxID = newStatusList.Max(s => s.Id);
            }
            RaiseNewMessageReceivedEvent(newStatusList);
        }

        protected override void FetchMessage()
        {
            List<Status> tempStatus = GetPublicStatuses();
            LastMaxID = tempStatus.Max(s => s.Id);
            RaiseMessageFetchedEvent(tempStatus);
        }

        public override List<Status> GetNextPageMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetPreviousePageMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetFirstPageMessage()
        {
            throw new NotImplementedException();
        }
    }
}
